package site.jlopen.kuscia.rpc.impl;

import java.io.FileNotFoundException;

import org.secretflow.v1alpha1.common.Common;
import org.secretflow.v1alpha1.factory.KusciaAPIChannelFactory;
import org.secretflow.v1alpha1.kusciaapi.DomainRoute;
import org.secretflow.v1alpha1.kusciaapi.DomainRoute.BatchQueryDomainRouteStatusRequest;
import org.secretflow.v1alpha1.kusciaapi.DomainRoute.BatchQueryDomainRouteStatusResponse;
import org.secretflow.v1alpha1.kusciaapi.DomainRoute.CreateDomainRouteRequest;
import org.secretflow.v1alpha1.kusciaapi.DomainRoute.CreateDomainRouteResponse;
import org.secretflow.v1alpha1.kusciaapi.DomainRoute.DeleteDomainRouteRequest;
import org.secretflow.v1alpha1.kusciaapi.DomainRoute.DeleteDomainRouteResponse;
import org.secretflow.v1alpha1.kusciaapi.DomainRoute.QueryDomainRouteRequest;
import org.secretflow.v1alpha1.kusciaapi.DomainRoute.QueryDomainRouteResponse;
import org.secretflow.v1alpha1.kusciaapi.DomainRouteServiceGrpc;
import org.secretflow.v1alpha1.kusciaapi.DomainRouteServiceGrpc.DomainRouteServiceBlockingStub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import site.jlopen.kuscia.rpc.KusciaDomainRouteRpc;
import site.jlopen.kuscia.exception.SdkBaseException;
import site.jlopen.kuscia.properties.KusciaAPIProperties;

import io.grpc.ManagedChannel;

public class KusciaDomainRouteRpcImpl implements KusciaDomainRouteRpc {
	private static Logger logger = LoggerFactory.getLogger(KusciaDomainRouteRpcImpl.class);

	private DomainRouteServiceBlockingStub blockingStub;
	private ManagedChannel channel;

	public KusciaDomainRouteRpcImpl(KusciaAPIProperties properties) {
		try {
			KusciaAPIChannelFactory channelFactory = new KusciaAPIChannelFactory(properties.getAddress(),
					properties.getTokenFile(), properties.getTls(), properties.getProtocol());
			this.channel = channelFactory.newClientChannel();
			this.blockingStub = DomainRouteServiceGrpc.newBlockingStub(channel);
		} catch (Exception e) {
			if(e instanceof FileNotFoundException) {
				throw SdkBaseException.of("证书文件不存在", e);
			}
			throw SdkBaseException.of("Channel通讯异常", e);
		}
	}

	@Override
	public CreateDomainRouteResponse createDomainRoute(CreateDomainRouteRequest request) {
		try {
			DomainRoute.CreateDomainRouteResponse response = blockingStub.createDomainRoute(request);
			//checkResponse(response.getStatus());
			Common.Status status = response.getStatus();
			logger.info("getStatus：{}", status);
			return response;
		} catch (Exception e) {
			logger.error("创建节点路由异常：{}", e);
		} finally {
			if(null != channel) {
				channel.shutdownNow();
			}
		}
		return null;
	}

	@Override
	public DeleteDomainRouteResponse deleteDomainRoute(DeleteDomainRouteRequest request) {
		try {
			DomainRoute.DeleteDomainRouteResponse response = blockingStub.deleteDomainRoute(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("删除节点路由异常：{}", e);
		} finally {
			if(null != channel) {
				channel.shutdownNow();
			}
		}
		return null;
	}

	@Override
	public QueryDomainRouteResponse queryDomainRoute(QueryDomainRouteRequest request) {
		try {
			DomainRoute.QueryDomainRouteResponse response = blockingStub.queryDomainRoute(request);
			//checkResponse(response.getStatus());
	        return response;
		} catch (Exception e) {
			logger.error("查询节点路由异常：{}", e);
		} finally {
			if(null != channel) {
				channel.shutdownNow();
			}
		}
		return null;
	}

	@Override
	public BatchQueryDomainRouteStatusResponse batchQueryDomainRouteStatus(BatchQueryDomainRouteStatusRequest request) {
		try {
			BatchQueryDomainRouteStatusResponse response = blockingStub.batchQueryDomainRouteStatus(request);
			//checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("批量查询节点路由异常：{}", e);
		} finally {
			if(null != channel) {
				channel.shutdownNow();
			}
		}
		return null;
	}

}
